  <template>
  <view class="loginView">
    <image
      src="../../static/images/bg.png"
      mode=""
      class="modelNameImage"
    ></image>
    <view class="content">
      <view class="nameContent">
        <image src="../../static/images/Logo.png" mode=""></image>
        <h4>金圣龙 ERP 管理系统</h4>
        <p>让协作更高效</p>
      </view>
    </view>

    <view class="formLogin">
      <view class="top">
        <h4>登陆系统</h4>
        <view class="input-box">
          <view class="icon">+86</view>
          <input
            type="number"
            class="input-value"
            v-model="phone"
            placeholder="请输入你的手机号"
          />
        </view>
        <text class="phoneWarnMsg">{{ phoneMsg }}</text>
        <view class="next-btn">
          <button
            type="primary"
            :disabled="disabledFlag"
            @tap="nextBtn"
            :loading="loading"
          >
            {{ nextBtnTxt }}
          </button>
        </view>
        <view class="radioProtocol">
          <label class="radio">
            <radio
              value="r1"
              @tap="checkedChange"
              :checked="radioProtocol"
            />我已阅读并同意 <text>服务协议</text> 和
            <text>隐私政策</text>
          </label>
        </view>
      </view>

      <view class="bottom">
        <p>更多登录方式</p>
        <button type="default" @tap="oneClickLogin" class="fastLogin">
          一键登录
        </button>
      </view>
    </view>

    <uni-popup
      ref="popup"
      background-color="#fff"
      class="popup-box"
      :mask-click="false"
      borderRadius="48rpx 48rpx 0 0"
    >
      <view class="popup-content" type="bottom">
        <view class="popup-top">
          <h4>服务协议与隐私保护</h4>
          <p class="myRead">
            我已阅读并同意
            <text>&nbsp;服务协议&nbsp;</text>
            和
            <text>&nbsp; 隐私政策</text>
          </p>
        </view>
        <view class="popup-bottom">
          <button type="default" class="noAgreeLogin" @tap="noAgreeLoginFn">
            不同意
          </button>
          <button
            class="agreeLogin"
            type="primary"
            @tap="agreeLoginFn"
            :loading="popupAgreeLogin"
          >
            <text class="button-text">{{
              popupAgreeLogin ? '登录中' : '同意并登录'
            }}</text>
          </button>
        </view>
      </view>
    </uni-popup>

    <uni-popup
      ref="popupLoginType"
      background-color="#fff"
      class="popup-login-type-box"
      :mask-click="false"
      borderRadius="48rpx 48rpx 0 0"
    >
      <loginTypePopupVue
        :phone="phone"
        :loginType="loginType"
        @passLoginHandler="passLoginHandler"
        @codeLoginHandler="codeLoginHandler"
        @closeTypeLogin="closeTypeLogin"
      />
    </uni-popup>
  </view>
</template>

<script setup>
import { computed, reactive, ref, watch } from 'vue'
import loginTypePopupVue from '../../components/loginTypePopup/loginTypePopup.vue'
import { onLoad } from '@dcloudio/uni-app'
let phone = ref('18567282261')
let radioProtocol = ref(false)
let phoneMsg = ref('')
let popup = ref('')
let popupLoginType = ref('')
let loading = ref(false)
let nextBtnTxt = ref('下一步')
const loginType = reactive({
  passLogin: true,
  codeLogin: true,
})
let popupAgreeLogin = ref(false)
const checkedChange = () => {
  radioProtocol.value = !radioProtocol.value
}

const disabledFlag = computed(() => {
  if (phone.value.length == 11) {
    return false
  } else {
    return true
  }
})
const phoneCheck = (fn) => {
  let reg_tel =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
  if (!reg_tel.test(phone.value)) {
    phoneMsg.value = '手机号格式不对'
    closeTypeLogin()
  } else {
    if (phoneMsg.value) {
      phoneMsg.value = ''
    }
    fn()
  }
}

const passLoginHandler = (fn) => {
  phoneCheck(fn)
}
const codeLoginHandler = (fn) => {
  phoneCheck(fn)
}

// const passLogin = () => {
// 	let fn = () => {
// 		phoneMsg.value = ''
// 		uni.navigateTo({
// 			url: `/pages/login/passWordLogin/passWordLogin?BLi=${encodeURIComponent(JSON.stringify(phone.value))}`,
// 		})
// 	}
// 	phoneCheck(fn)
// }

// const codeLogin = () => {
// 	let fn = () => {
// 		phoneMsg.value = ''
// 		uni.navigateTo({
// 			url: `/pages/login/phoneLogin/phoneLogin?BLi=${encodeURIComponent(JSON.stringify(phone.value))}`,
// 		})
// 	}
// 	phoneCheck(fn)
// }
// 下一步
const nextBtn = () => {
  let fn = () => {
    loading.value = true
    phoneMsg.value = ''
    if (!radioProtocol.value) {
      toggle('bottom')
    }
  }
  phoneCheck(fn)
}
const oneClickLogin = () => {
  popupLoginType.value.open('bottom')
}
// 底部弹窗
const toggle = (type) => {
  nextBtnTxt = '登录中'
  popup.value.open(type)
}

// 弹窗登录
const agreeLoginFn = () => {
  radioProtocol.value = true
  popupAgreeLogin.value = true

  uni.navigateTo({
    url: `/pages/login/passWordLogin/passWordLogin?BLi=${encodeURIComponent(JSON.stringify(phone.value))}`,
  })
}

// 弹窗不同意
const noAgreeLoginFn = () => {
  loading.value = false
  popup.value.close()
}
const closeTypeLogin = () => {
  popupLoginType.value.close()
}

onLoad(() => {
  uni.loadFontFace({
    family: 'jinbu',
    source: 'url("/static/font/DingTalk JinBuTi.ttf")',
  })

  // #ifdef APP-PLUS
  uni.loadFontFace({
    family: 'jinbu',
    source: `url(${plus.io.convertLocalFileSystemURL('/static/font/DingTalk JinBuTi.ttf')})`,
  })
  // #endif
})
</script>

<style scoped lang="scss">
.loginView {
  width: 100%;
  height: 100vh;
  position: relative;

  :deep(uni-button[disabled]) {
    background-color: rgba(0, 122, 255, 0.3);
  }

  .modelNameImage {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-direction: column;
    width: 100%;
    height: 40%;
    padding-top: 124rpx;
    padding-bottom: 108rpx;
    box-sizing: border-box;

    .nameContent {
      display: flex;
      align-items: center;
      flex-direction: column;

      image {
        width: 184rpx;
        height: 184rpx;
      }

      h4 {
        margin-top: 40rpx;
        font-family: 'jinbu';
        font-size: 56rpx;
        font-style: normal;
        font-weight: 400;
        line-height: 78.4rpx;
        background: linear-gradient(180deg, #1d2129 0%, #1d2129b8 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }

      p {
        color: #86909c;
        font-family: 'PingFang SC';
        font-size: 32rpx;
        font-style: normal;
        font-weight: 300;
        line-height: 44.8rpx;
        letter-spacing: 12.8rpx;
      }
    }
  }

  .formLogin {
    background-color: #fff;
    position: absolute;
    width: 100%;
    height: 60%;
    box-shadow: 0 -12rpx 72rpx 0 #0000000d;
    border-top-left-radius: 60rpx;
    border-top-right-radius: 60rpx;
    padding: 32rpx;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    h4 {
      color: #1d2129;
      font-size: 48rpx;
      font-weight: 500;
      line-height: 67.2rpx;
    }

    .top {
      .input-box {
        display: flex;
        align-items: center;
        border: 1px solid #e5e6eb;
        border-radius: 12rpx;
        height: 88rpx;
        margin-top: 32rpx;

        .icon {
          padding: 22rpx 24rpx 22rpx 22rpx;
          border-right: 1px solid #e5e6eb;
        }

        .input-value {
          padding-left: 20rpx;
        }
      }

      .phoneWarnMsg {
        color: red;
        font-size: 24rpx;
      }

      .next-btn {
        margin-top: 32rpx;

        // :deep(uni-button[disabled]) {
        // 	background-color: rgba(0, 122, 255, 0.3);
        // }
      }

      .radioProtocol {
        margin-top: 32rpx;
        color: #86909c;

        :deep(.radio) {
          display: flex;
          align-items: center;

          uni-radio-input {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 22px;
            height: 22px;
          }

          span {
            color: #165dff;
          }
        }
      }
    }

    .bottom {
      margin-bottom: 108rpx;

      p {
        text-align: center;
        position: relative;
        background-color: #fff;
        color: #86909c;
        font-size: 24rpx;
        font-weight: 400;
        line-height: 33.6rpx;

        &:after {
          content: '';
          width: 37%;
          height: 1px;
          border-bottom: 1px solid #f7f8fa;
          display: inline-block;
          position: absolute;
          top: 16rpx;
          right: 0px;
        }

        &:before {
          content: '';
          width: 37%;
          height: 1px;
          border-bottom: 1px solid #f7f8fa;
          display: inline-block;
          position: absolute;
          top: 16rpx;
          left: 0px;
        }
      }

      .fastLogin {
        background-color: #ffffff;
        color: #1d2129;
        font-weight: 400;
        margin-top: 20rpx;
      }
    }
  }

  .popup-box {
    :deep(.popup-content) {
      height: 30vh;
      padding: 40rpx 32rpx 0 32rpx;
      display: flex;
      flex-direction: column;

      .popup-top {
        flex: 1;

        h4 {
          font-family: 'PingFang SC';
          margin-bottom: 32rpx;
          color: #1d2129;
          font-size: 36rpx;
          font-style: normal;
          font-weight: 500;
        }

        .myRead {
          color: #86909c;
          font-family: 'PingFang SC';
          font-size: 32rpx;
          font-style: normal;
          font-weight: 400;
        }
      }

      .popup-bottom {
        display: flex;
        align-items: center;
        justify-content: space-between;
        font-size: 30rpx;
        flex: 3;

        .noAgreeLogin {
          width: 270rpx;
          margin: 0;
          background-color: #fff;
        }

        .agreeLogin {
          width: 384rpx;
          margin: 0;
          background-color: #165dff;
        }
      }
    }
  }
}
</style>
